<template>
  <div class="image-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="radio-header">
        <el-radio-group
          v-model="collect"
          size="mini"
          @change="statusChangeAction"
        >
          <el-radio-button :label="false">全部</el-radio-button>
          <el-radio-button :label="true">收藏</el-radio-button>
        </el-radio-group>
        <el-button
          size="mini"
          type="success"
          @click="dialogUploadVisible = true"
          >上传素材</el-button
        >
      </div>

      <el-row :gutter="10">
        <el-col
          :xs="12"
          :sm="6"
          :md="6"
          :lg="4"
          v-for="item in images"
          :key="item.id"
          class="col-img"
        >
          <el-image
            style="width: 100%; height: 200px"
            :src="item.url"
            fit="cover"
          ></el-image>
          <div class="option-bt">
            <el-button
              class="bt delete"
              icon="el-icon-delete"
              size="medium"
              :loading="item.delLoading"
              @click="deleteImgAction(item)"
            ></el-button>
            <el-button
              type="plain"
              class="bt"
              :class="{ 'collect': item.is_collected }"
              :icon=" item.is_collected?'el-icon-star-on':'el-icon-star-off'"
              :loading="item.colLoading"
              @click="collectImageAction(item)"
            ></el-button>
          </div>
        </el-col>
      </el-row>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        :page-size="pageSize"
        :current-page.sync="page"
        @current-change="onCurrentChange"
        :disabled="pagenationDisabled"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="上传素材"
      :append-to-body="true"
      :visible.sync="dialogUploadVisible"
    >
      <el-upload
        v-if="dialogUploadVisible"
        class="upload-demo"
        drag
        action="http://api-toutiao-web.itheima.net/mp/v1_0/user/images"
        :headers="uploadHeader"
        name="image"
        :multiple="true"
        :on-success="uploadSuccess"
        :before-upload="beforeUpload"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          只能上传jpg/png文件，且不超过500kb
        </div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
// uploadImage
import { getImages, collectImg, deleteImg } from '@/api/image'
export default {
  name: 'ImageIndex',
  data () {
    // 设置统一的token
    const user = JSON.parse(window.localStorage.getItem('user'))
    return {
      collect: false,
      url: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
      images: [],
      dialogUploadVisible: false,
      uploadHeader: {
        Authorization: `Bearer ${user.token}`
      },
      page: 1,
      totalCount: 0,
      pageSize: 10,
      pagenationDisabled: false
    }
  },
  props: {},
  components: {},
  mounted () {},
  created () {
    this.loadImages()
  },
  methods: {
    loadImages () {
      const params = {
        page: this.page,
        per_page: 10,
        collect: this.collect
      }
      getImages(params)
        .then((res) => {
          const { results } = res.data.data
          results.forEach(item => {
            item.colLoading = false
            item.delLoading = false
          })
          this.images = results
          this.totalCount = res.data.data.total_count
        })
        .catch((e) => {})
    },
    statusChangeAction (e) {
      this.page = 1
      this.loadImages()
    },
    uploadSuccess () {
      this.page = 1
      this.loadImages()
    },
    beforeUpload (file) {},

    onCurrentChange (e) {
      this.loadImages()
    },
    // 收藏图片
    collectImageAction (img) {
      img.colLoading = true
      collectImg(img.id, !img.is_collected).then((res) => {
        this.$message({
          message: img.is_collected ? '取消收藏' : '收藏成功',
          type: 'success'
        })
        img.colLoading = false
        this.loadImages()
      }).catch(e => {
        img.colLoading = false
      })
    },
    // 删除图片
    deleteImgAction (img) {
      img.delLoading = true
      deleteImg(img.id).then((res) => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        img.delLoading = false
        this.loadImages()
      }).catch(e => {
        img.delLoading = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.radio-header {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 20px;
}

.option-bt {
  position: absolute;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
  height: 40px;
  left: 5px;
  right: 5px;
  bottom: 0;
  background-color: rgba(204, 204, 204, 0.5);
  font-size: 20px;
  color: #fff;
}

.collect {
  color: greenyellow;
  font-size: 24px;
}

.bt {
  background-color: transparent;
  border: 0;
}

.bt:hover {
  background-color: transparent;
  color: #ffffff;
}

.delete {
  color: red;
  font-size: 20px;
}

.col-img {
  position: relative;
}
</style>
